#ifndef __LOGO_WND_H__
#define __LOGO_WND_H__

#include "resource.h"
#include "SimpleWnd/SimpleWnd.h"
#include "SimpleWnd/FrameWnd.h"
#include "DialogBar.h"
#include "Commander.h"
#include "SplitterWnd.h"
#include "edit/EditWindow.h"
#include "LogoDisplayPane.h"
#include "logolib/logo_lib.h"

#define WINDOW_CLASS_STR "__CZF_Trophy_My_Logo__"
#define WINDOW_NAME_STR "Trophy Logo"

#define LOGO_TMP_FILE "tmpsrc.logo"

#define CHECKING_TIMER  (100)

class CLogoBkPtMgr : public IBkPtMgr
{
public:
   virtual void ToggleBreakPoint (unsigned int lineno) 
   {
      assert (m_hexe);
      if (*m_hexe) {
         debug_toggle_breakpoint(*m_hexe, lineno);
      }
   }
   virtual bool IsBreakPoint (unsigned int lineno)
   {
      assert (m_hexe);
      if (*m_hexe) {
         return debug_find_breakpoint(*m_hexe, lineno);
      }
      return false;
   }
   virtual void AdjustBreakPoints (bool insert, unsigned int lineno)
   {
      assert (m_hexe);
      if (*m_hexe) {
         debug_adjust_breakpoints(*m_hexe, insert, lineno);
      }
   }
   virtual void ClearAllBreakPoints()
   {
      assert (m_hexe);
      if (*m_hexe) {
         debug_free_breakpointlist(*m_hexe);
      }
   }
   exe_handle_t*  m_hexe;
};

typedef struct tagExecInfo {
    LPCSTR        pFilename;
    HWND          hCanvas;
    exe_handle_t  hexe;
} ExecInfo;

class CLogoWnd : public CFrameWnd,
    public IExecCommander
{
public:
    DECLARE_WND_CLASS_EX(WINDOW_CLASS_STR, 0, COLOR_3DSHADOW)

    CLogoWnd();
    virtual ~CLogoWnd();

BEGIN_MSG_MAP(CLogoWnd)
    MESSAGE_HANDLER(WM_CREATE, OnCreate)
    MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
    MESSAGE_HANDLER(WM_CLOSE, OnClose)
    // custom message from inplace editor
    COMMAND_ID_HANDLER(IDM_EXIT, OnExit)
    COMMAND_ID_HANDLER(IDM_FILE_OPEN, OnFileOpen)
    COMMAND_ID_HANDLER(IDM_FILE_CLOSE, OnFileClose)
    COMMAND_ID_HANDLER(IDM_FILE_SAVE, OnFileSave)
    COMMAND_ID_HANDLER(IDM_FILE_SAVEAS, OnFileSaveAs)
    COMMAND_ID_HANDLER(IDM_EXEC_RUN, OnExecRun)
    COMMAND_ID_HANDLER(IDM_EXEC_ABORT, OnExecAbort)
    COMMAND_ID_HANDLER(IDM_DEBUG_GO, OnDebugGo)
    COMMAND_ID_HANDLER(IDM_DEBUG_STOP, OnExecAbort)
    COMMAND_ID_HANDLER(IDM_DEBUG_STEPTO, OnDebugStepTo)
    COMMAND_ID_HANDLER(IDM_DEBUG_STEPOVER, OnDebugStepOver)
    COMMAND_ID_HANDLER(IDM_DEBUG_STEPOUT, OnDebugStepOut)
    COMMAND_ID_HANDLER(IDM_DEBUG_STEPINTO, OnDebugStepInto)
    COMMAND_ID_HANDLER(IDM_DEBUG_BREAKPOINT, OnDebugToggleBreakPoint)
    COMMAND_ID_HANDLER(IDM_CONFIG, OnConfig)
    COMMAND_ID_HANDLER(ID_COLORPICKER, OnColorPicker)
    CHAIN_MSG_MAP(CFrameWnd)
END_MSG_MAP()

protected:
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnFileClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnFileSave(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnFileSaveAs(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnExecRun(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnExecAbort(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugGo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugStepTo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugStepOver(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugStepOut(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugStepInto(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnDebugToggleBreakPoint(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnConfig(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnColorPicker(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

    LRESULT RunIt(bool debug=false);
    void EnableExecutionMenu();

    /* Check menu helpers. */
    void CheckRunAndAbortMenus ();
    void CheckDebugGoAndStepToMenus();
    void CheckDebugStopMenu ();
    void CheckOtherDebugMenus ();

public:
    HWND CreateLogoWnd(RECT* pRect);
    // interface IExecCommander
    virtual void ExecutionDone(void);
    virtual void DebugCommandFinished(unsigned int lineno);

    void ReadFile(LPCTSTR lpfn);

protected:
    HMENU m_hMenu;
    HICON m_hIcon;   
    CSplitterWindow m_splitter;
    CEditWindow m_edit;
    CLogoDisplayPane m_right;
    char m_fn[_MAX_PATH];
    char m_szConfigDir[_MAX_PATH];
    bool m_bRunning;
    bool m_bDebugCmdAcked;

    CToolBar    m_tbDebug;
    CToolBar    m_tbFile;
    CDialogBar  m_dlgBar;
    CLogoBkPtMgr* m_pBkPtMgr;
    ExecInfo    m_exeInfo;
};

void ExecThread (void* pData);

#endif // __LOGO_WND_H__